package com.jialanmo.helper.recyclerview;

import android.animation.ObjectAnimator;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

/**
 * RecyclerView侧滑菜单栏适配器
 * <br/>
 * 需要设置recyclerview_item模板layout文件里根view的方向为横向(默认)
 * <p/>
 * 第二个子view为{@link ViewGroup}的子类,将button等放在里面
 */
public final class ItemSlideListener extends RecyclerView.SimpleOnItemTouchListener {

    private final RecyclerView mRecyclerView;
    private final OnMenuItemClickListener mOnMenuItemClickListener;

    private int mLastX;
    private int mLastY;
    private View mTargetView;
    private boolean isExpand;

    private ObjectAnimator mObjectAnimator;

    public ItemSlideListener(RecyclerView recyclerView, OnMenuItemClickListener onMenuItemClickListener) {
        mRecyclerView = recyclerView;
        mOnMenuItemClickListener = onMenuItemClickListener;
    }

    @Override
    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
        if (mObjectAnimator != null && mObjectAnimator.isRunning()) {
            return true;
        }
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastX = (int) e.getX();
                mLastY = (int) e.getY();
                View view = mRecyclerView.findChildViewUnder(mLastX, mLastY);
                if (mTargetView == null) {
                    //点击一个新的item
                    mTargetView = view;
                } else {
                    //此时已经有展开的菜单了,判断点击的是否是当前展开的菜单
                    if (mTargetView == view) {
                        //点击的是当前展开的菜单,判断是否点击的是菜单
                        //如果不是菜单,拦截点击事件,是菜单则进入action_up判断
                        return getMenuViewIndex((int) e.getX(), (int) e.getY()) == -1;
                    } else {
                        //点击的不是当前展开的菜单,折叠已经展开的菜单
                        collapseView();
                        return true;
                    }
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = (int) e.getX() - mLastX;
                int deltaY = (int) e.getY() - mLastY;
                if (Math.abs(deltaX) > Math.abs(deltaY) && mTargetView != null && Math.abs(deltaX) > 8) {
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
                //没有滑动事件需要拦截
                if (mTargetView != null && mTargetView.getScrollX() > 0) {
                    //有菜单已经展开
                    int index = getMenuViewIndex((int) e.getX(), (int) e.getY());
                    if (index > -1 && mOnMenuItemClickListener != null) {
                        //触发点击菜单事件
                        mOnMenuItemClickListener.onMenuItemClick(this, index, mRecyclerView.getChildAdapterPosition(mTargetView));
                    } else {
                        //点击处不是菜单,折叠菜单
                        collapseView();
                    }
                    //此处不拦截会触发当前item的clicklistener
                    return true;
                }
                break;
        }
        return false;
    }

    @Override
    public void onTouchEvent(RecyclerView rv, MotionEvent e) {
        if (mObjectAnimator != null && mObjectAnimator.isRunning() || mTargetView == null) {
            return;
        }
        int scrollX = mTargetView.getScrollX();
        int maxWidth = ((LinearLayout) mTargetView).getChildAt(1).getWidth();
        switch (e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                int deltaX = mLastX - (int) e.getX();
                if (scrollX + deltaX <= 0) {
                    mTargetView.scrollTo(0, mTargetView.getScrollY());
                } else if (scrollX + deltaX < maxWidth) {
                    mTargetView.scrollBy(deltaX, 0);
                } else {
                    mTargetView.scrollBy(0, 0);
                }
                mLastX = (int) e.getX();
                break;
            case MotionEvent.ACTION_UP:
                if (isExpand) {
                    if (maxWidth - scrollX > maxWidth / 4) {
                        collapseView();
                    } else {
                        expandView();
                    }
                } else {
                    if (scrollX < maxWidth / 4) {
                        collapseView();
                    } else {
                        expandView();
                    }
                }
                break;
        }
    }

    private int getMenuViewIndex(int x, int y) {
        ViewGroup viewGroup = (ViewGroup) ((ViewGroup) mTargetView).getChildAt(1);
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View view = viewGroup.getChildAt(i);
            int left = mTargetView.getWidth() - mTargetView.getScrollX() + view.getLeft();
            int top = mTargetView.getTop() + view.getTop();
            int right = left + view.getWidth();
            int bottom = top + view.getHeight();
            if (new Rect(left, top, right, bottom).contains(x, y)) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 展开菜单
     */
    public void expandView() {
        mObjectAnimator = ObjectAnimator.ofInt(mTargetView, "scrollX", ((LinearLayout) mTargetView).getChildAt(1).getWidth()).setDuration(200);
        mObjectAnimator.start();
        isExpand = true;
    }

    /**
     * 折叠菜单
     */
    public void collapseView() {
        mObjectAnimator = ObjectAnimator.ofInt(mTargetView, "scrollX", 0).setDuration(200);
        mObjectAnimator.start();
        mTargetView = null;
        isExpand = false;
    }
}